﻿using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Microsoft.WindowsAzure.ServiceRuntime;

namespace McHotspoot
{
    public class CSharp : McHotspotI
    {

        public string execute(string key, double n, double index, string codein, out string message)
        {       
            Mono.CSharp.Evaluator.Run("using System;");
            Mono.CSharp.Evaluator.Run("double index = " + index + ";");
            Mono.CSharp.Evaluator.Run("double n = " + n + ";");
            string r = (string)Mono.CSharp.Evaluator.Evaluate(codein.TrimEnd());
            message = "Code: " + codein + "\r\n" + "Result: " + r;
            return r;
        }

        public string finish(string key, double n, string r, string codein, string codeout, out string message)
        {            
            Mono.CSharp.Evaluator.Run("using System;");
            Mono.CSharp.Evaluator.Run("using System.Text;");
            Mono.CSharp.Evaluator.Run("using System.IO;");
            Mono.CSharp.Evaluator.Run("double n = " + n.ToString() + ";");
            Mono.CSharp.Evaluator.Run("string r = @" + "\"" + r + "\"" + ";");
            string m = (string)Mono.CSharp.Evaluator.Evaluate(codeout.TrimEnd());
            message = "Code: " + codeout + "\r\n" + "Result: " + m;
            return m;
        }

        public string test(string key, double n, string codein, string codeout, double npt)
        {
            try{
                bool ok = true;
                if (n < 0) ok = false;
                if (Math.Truncate(n) < n) ok = false;
                if (npt < 0) ok = false;
                if (Math.Truncate(npt) < npt) ok = false;
                if (npt > n) ok = false;
                if (!ok) return "Error: Invalid input!";

                string message = "";
                double x = 2;                
                string r = execute(key, x, 1, codein, out message);
                string filesRoot = RoleEnvironment.GetLocalResource("McCloudStorage").RootPath;
                string file = Path.Combine(filesRoot, key + ".temp");
                FileStream fileStream = new FileStream(file, FileMode.Create);
                byte[] bytes = Encoding.Default.GetBytes(r);
                fileStream.Write(bytes, 0, bytes.Length);
                fileStream.Close();
                string m = finish(key, x, file, codein, codeout, out message);
                if (m != "") return "";
                else return "Error: Null Result";
            }
            catch(Exception e) { return "Error: "+e.ToString(); }
        }

        public void optimization(double n, string codein, string codeout, double npt, 
            out double ninstancesadded, out double ntasks, out int timeoutInSeconds)
        {
            if (npt != 0)
            {
                ntasks = (double)Math.Truncate((decimal)(n / npt));
                if (n > (ntasks * npt)) ntasks++;
            }
            else
            {
                ntasks = 19;
            }
            ninstancesadded = 0;
            timeoutInSeconds = 2 * 60 * 60;
        }
    }
}